package com.atguigu.paymentdemo.util;

import com.alibaba.fastjson.JSONException;
import com.github.qcloudsms.*;
import com.github.qcloudsms.httpclient.HTTPException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
/******************************
 *描述：腾讯云短信平台操作相关
 *日期：2019/11/26
 *作者：yumiao
 *
 *
 ******************************/

public class TencentSMS {
    @Autowired
    HttpServletRequest request;
    @Autowired
    HttpServletResponse response;

    //初始化信息
    public static String sendSMS(String phoneNumber,String code){

        boolean flag=true;
        String reStr = ""; //定义返回值
        // 短信应用SDK AppID  // 1400开头
        int appid =1400947531 ;
        // 短信应用SDK AppKey
        String appkey = "SiolXArYLJ8k8HTbYKvMbvCVmusoXOAj";
        // 短信模板ID，需要在短信应用中申请
        int templateId = 2308574;
        // 签名，使用的是`签名内容`，而不是`签名ID`
        String smsSign = "五三里官方网站";
        try {
            //参数，一定要对应短信模板中的参数顺序和个数，
            String[] params = {code};
            //创建ssender对象
            SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
            //发送
            SmsSingleSenderResult result = ssender.sendWithParam("86", phoneNumber,templateId, params, smsSign, "", "");
            // 签名参数未提供或者为空时，会使用默认签名发送短信
            System.out.println(result.toString());
            if(result.result==0){
                reStr = "success";
            }else{
                reStr = "error";
            }
        } catch (HTTPException e) {
            // HTTP响应码错误
            e.printStackTrace();
        } catch (JSONException e) {
            // json解析错误
            e.printStackTrace();
        } catch (IOException e) {
            // 网络IO错误
            e.printStackTrace();
        }catch (Exception e) {
            // 网络IO错误
            e.printStackTrace();
        }
        return reStr;

    }

}
